﻿//-----------------------------------------------------------------------
// <copyright file="Startup.cs" company="SLWY Enterprises">
// * Copyright (C) 2017 四川商旅无忧科技有限公司 版权所有。
// * version : 1.0
// * author  : qinchaoyue
// * FileName: Startup.cs
// * history : created by qinchaoyue 2017-10-13 05:01:17
// </copyright>
//-----------------------------------------------------------------------

using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Slwy.MySql;

namespace WebApplication
{
    using Microsoft.Extensions.DependencyInjection;
    using Slwy.EventBus;
    using Slwy.FrameCore.Dependency;

    using IServiceCollection = Microsoft.Extensions.DependencyInjection.IServiceCollection;

    /// <summary>
    /// The startup.
    /// </summary>
    public class Startup
    {
        public Startup(IConfiguration configuration, IHostingEnvironment environment)
        {
            this.Configuration = configuration;
            this.CurrentEnvironment = environment;
        }

        /// <summary>
        /// Gets the configuration.
        /// </summary>
        public IConfiguration Configuration { get; }

        /// <summary>
        /// Gets the ccurrent environment.
        /// </summary>
        public IHostingEnvironment CurrentEnvironment { get; }

        /// <summary>
        /// Di 组件替换成 AutoFac 
        /// </summary>
        /// <param name="services"> 服务集合 . </param>
        /// <returns> 服务集合 </returns>
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            ////services.AddCors(options => {
            ////        options.AddPolicy("any", builder => {
            ////                builder.AllowAnyOrigin()
            ////                    .AllowAnyMethod()
            ////                    .AllowAnyHeader()
            ////                    .AllowCredentials();
            ////            });
            ////    });

            // 注册 mvc 服务 包含服务发现和文档服务, 
            Slwy.Mvc.Startup.ConfigureServices(services, this.Configuration, this.CurrentEnvironment);

            // 注册 mysql 服务
            services.AddMySqlService();

            // 添加 autoFac 支持
            return new ServicesBuilder().Build(services, this.Configuration);
        }

        /// <summary>
        /// The configure.
        /// </summary>
        /// <param name="app"> The app.  </param>
        /// <param name="env"> The env.  </param>
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            // 框架配置
            ServicesBuilder.Configure(app, env);

            // mvc 配置
            Slwy.Mvc.Startup.Configure(app, env);
            app.UseEventBus();
        }
    }
}
